package org.neuedu.ssmp.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import org.neuedu.ssmp.crm.entity.*;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.mapper.ServeListMapper;
import org.neuedu.ssmp.crm.mapper.ServeStateMapper;
import org.neuedu.ssmp.crm.mapper.SysUserMapper;
import org.neuedu.ssmp.crm.service.IServeListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MoonHolder
 * @since 2019-12-09
 */
@Service
public class ServeListServiceImpl extends ServiceImpl<ServeListMapper, ServeList> implements IServeListService {
    @Autowired
    private ServeListMapper serveListMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ServeStateMapper stateMapper;

    @Override
    public ResponseEntity edit(ServiceManagementHandle sm) {
//        LocalDate grantTime = null,operatorTime = null;
//        if(Objects.nonNull(sm.getHandlerTime())){
//            operatorTime = LocalDate.now();
//        }
//        if (Objects.nonNull(sm.getAssignedTime())){
//            grantTime = LocalDate.now();
//        }
        ServeList serveList = ServeList.builder().id(sm.getId()).state(sm.getState()).userId(sm.getAllotfor())
                .operatorId(sm.getHandlerId()).operatorContent(sm.getOperatorContent()).grantTime(sm.getAssignedTime())
                .operatorTime(sm.getHandlerTime()).build();
        if (baseMapper.updateById(serveList) == 1) {
            return ResponseEntity.ok("成功");
        }
        return ResponseEntity.error("失败");
    }

    @Override
    public ResponseEntity del(int id) {
        if (baseMapper.deleteById(id) != 0) {
            return ResponseEntity.ok("删除成功！");
        }
        return ResponseEntity.error("删除失败！");
    }

    @Override
    public List<ServiceManagementHandle> conditionQuery(String name, String summry, String createTime, Integer from,Integer userId) {
        QueryWrapper<ServiceManagementHandle> queryWrapper = new QueryWrapper<ServiceManagementHandle>()
                .like(Objects.nonNull(name), "cr.name", name)
                .like(Objects.nonNull(summry), "summary", summry)
                .like(Objects.nonNull(createTime), "create_time", createTime)
                .eq(Objects.nonNull(userId),"sl.user_id",userId)
                .in("state", from);
        return baseMapper.conditionQuery(queryWrapper);
    }

    @Override
    public ResponseEntity add(ServeList serveList) {
        if (baseMapper.insert(serveList) == 1) {
            return ResponseEntity.ok("添加成功");
        }
        return ResponseEntity.error("添加失败");
    }

    @Override
    public ResponseEntity sysUserInfo() {
        List<SysUser> userList = new LambdaQueryChainWrapper<>(userMapper).eq(SysUser::getRoleId, 3).list();
        return ResponseEntity.data(userList);
    }

    @Override
    public ResponseEntity severStateInfo() {
        List<ServeState> statelist = new LambdaQueryChainWrapper<>(stateMapper).eq(ServeState::getId, 1).list();
        return ResponseEntity.data(statelist);
    }

    @Override
    public List<ServeList> serviceArchiving() {
        return serveListMapper.serviceArchiving();
    }

    @Override
    public List<ServiceManagement> findAllsServiceArchiving() {
        return serveListMapper.findAllsServiceArchiving();
    }

    @Override
    public List<ServiceManagement> findAllsSsrviceFeedback() {
        return serveListMapper.findAllsSsrviceFeedback();
    }

    @Override
    public List<ServiceManagement> serviceArchive(String inputClientName, String summry, String createTime) {
        return serveListMapper.serviceArchiveSearch(inputClientName, summry, createTime);
    }

    @Override
    public List<ServiceManagement> serviceFeedback(String inputClientName, String summry, String createTime) {
        return serveListMapper.serviceFeedbackSearch(inputClientName, summry, createTime);
    }
}
